#!/usr/bin/env python
# coding=utf-8
from __future__ import absolute_import

import tornado.web

import WrapCursor
from CommonLib import ABCMeta, json, CheckJsonKeys, CheckAuth
from database.models.DatabaseCommon import *
from database.models.MasterInfo import MasterInfo


class GetMasterInfoHandler(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        r = WrapCursor.WrapCursor()
        try:
            ojson = json.decoder(self.request.body)
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except Exception as e:
            self.write(r.Error('request data error:' + str(e.message)))

        s = Session()
        c = s.query(MasterInfo).all()
        retjson = dict()
        retjson['Code'] = 0
        retjson['desc'] = u'成功'
        data = list()
        for i in c:
            o = {'key': i.name, 'val': i.value}
            data.append(o)
        retjson['data'] = data
        s = json.dumps(retjson)
        self.write(s)
